package gui.tools.actions;

import gui.Frame;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import alogrithm.NearestNeighbor;

public class NearestNeighborAction extends AbstractAction
{
   private static Icon icon = new ImageIcon("icons/nn.png");
   /**
    * 
    */
   private static final long serialVersionUID = 1L;
   private Frame frame;
   public NearestNeighborAction(Frame frame)
   {
      super("Nearest Neighbor",icon);
      this.frame = frame;
   }

   @Override
   public void actionPerformed(ActionEvent arg0)
   {
      // TODO Auto-generated method stub
      frame.removeListeners();
      frame.getView().reset();

      if(frame.getView().isAllNodeConnected()==false)
      {
         JOptionPane.showMessageDialog(frame,
               "Please connect all unconnected nodes.");
         frame.getView().repaint();
      }

      else if (frame.getView().getVertices().size() > 1
            && frame.getView().getEdges().size() > 0)
      {
         if(frame.getView().getVertices().size()>20
               || frame.getView().getEdges().size()>20)
            JOptionPane.showMessageDialog(frame,
                  "Nodes or Edges is greater than 20, make applicastion unstable",
                  "Unstable Warming",
                  JOptionPane.WARNING_MESSAGE);
         new NearestNeighbor(frame.getView());
         frame.getView().repaint();
      }
      else
         JOptionPane.showMessageDialog(frame,
               "Please insert Node and Connector " +
                     "from the Insert Menu",
                     "Error",
                     JOptionPane.ERROR_MESSAGE);
   }

}
